package com.example.partyfee.config;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import org.springframework.context.annotation.Configuration;

import java.io.InputStream;
import java.io.OutputStream;

@Configuration
public class ExcelConfig {
    
    /**
     * 获取Excel读取构建器
     *
     * @param inputStream 输入流
     * @param clazz 目标类型
     * @param <T> 泛型类型
     * @return Excel读取构建器
     */
    public static <T> ExcelReaderBuilder getReadBuilder(InputStream inputStream, Class<T> clazz) {
        return EasyExcel.read(inputStream)
                .head(clazz)
                .autoCloseStream(true);
    }

    /**
     * 获取Excel写入构建器
     *
     * @param outputStream 输出流
     * @param clazz 目标类型
     * @param <T> 泛型类型
     * @return Excel写入构建器
     */
    public static <T> ExcelWriterBuilder getWriteBuilder(OutputStream outputStream, Class<T> clazz) {
        return EasyExcel.write(outputStream, clazz)
                .autoCloseStream(true)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()); // 自适应列宽
    }
} 